package com.study.base.cas;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @description: AtomicReferenceDemo  源自引用
 * @date: 2021/1/28 18:57
 * @author: hcm
 * @version: 1.0
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User user1 = new User("张三",25);
        User user2 = new User("李四",13);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);
        System.out.println(atomicReference.compareAndSet(user1, user2)+"\t"+atomicReference.get().getUserName().toString());
        System.out.println(atomicReference.compareAndSet(user1, user2)+"\t"+atomicReference.get().getUserName().toString());
    }
}

class User{
    String userName;
    int age;

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    public User() {
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}